<template>
  <div class="app-container">
    机器人 {{ robotName }},({{ robotId }})
    <el-table
      :data="logs"
      style="width: 100%"
      max-height5="600"
      size="mini"
    >
      <el-table-column
        prop="time"
        label="时间"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="类型"
      >
        <template slot-scope="scope">
          {{ scope.row.logType }}/{{ getLogLv(scope.row.logLevel) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="msg"
        label="内容"
        min-width="300"
        show-overflow-tooltip5
      />
      <el-table-column
        prop="ParentActId"
        label="pid"
      />
      <el-table-column
        prop="info1"
        label="信息1"
      />
      <el-table-column
        prop="info2"
        label="信息2"
      />
      <el-table-column
        prop="info3"
        label="信息3"
      />
    </el-table>
  </div>
</template>

<script>
import sapi from '@/api/sysApi'
import * as signalR from '@microsoft/signalr'
// import robot from '@/api/robot'
export default {
  data() {
    return {
      loading: false,
      robotId: '',
      robotName: '',
      logs: [],
      logTotal: 0,
      periodList: [
        { loading: false, name: '1m', period: 60, sign: 'OneMinute,' },
        { loading: false, name: '"3m"', period: 3 * 60, sign: 'ThreeMinutes,' },
        { loading: false, name: '"5m"', period: 5 * 60, sign: 'FiveMinutes,' },
        { loading: false, name: '"15m"', period: 15 * 60, sign: 'FifteenMinutes,' },
        { loading: false, name: '"30m"', period: 30 * 60, sign: 'ThirtyMinutes,' },
        { loading: false, name: '"1h"', period: 3600, sign: 'OneHour,' },
        { loading: false, name: '"2h"', period: 2 * 3600, sign: 'TwoHours,' },
        { loading: false, name: '"4h"', period: 4 * 3600, sign: 'FourHours,' },
        { loading: false, name: '"6h"', period: 6 * 3600, sign: 'SixHours,' },
        { loading: false, name: '"8h"', period: 8 * 3600, sign: 'EightHours,' },
        { loading: false, name: '"12h"', period: 12 * 3600, sign: 'TwelveHours,' },
        { loading: false, name: '"1d"', period: 24 * 3600, sign: 'OneDay,' },
        { loading: false, name: '"3d"', period: 3 * 24 * 3600, sign: 'ThreeDays,' },
        { loading: false, name: '"1w"', period: 7 * 24 * 3600, sign: 'OneWeek,' },
        { loading: false, name: '"1M"', period: 30 * 24 * 3600, sign: 'OneMonth' }
      ]
    }
  },
  computed: {

  },
  created() {
    this.robotId = parseInt(this.$route.query.robotId)
    this.robotName = this.$route.query.robotName
    this.getLogs()
  },
  mounted() {

  },
  methods: {
    getLogLv(lv) {
      switch (lv) {
        case 1:return 'Error'
        case 2:return 'Warn'
        case 3:return 'Info'
        case 4:return 'Debug'
        default:
          return ''
      }
    },
    getLogs(item) {
      sapi.getRobotLogs({ robotId: this.robotId, pageNum: 1, pageSize: 50 }).then((p) => {
        this.logTotal = p.data.item1
        this.logs = p.data.item2
      }).then(p => this.logSubs())
    },
    async logSubs() {
      const hubUrl = this.appGlobal.SvcBaseUrl + '/ohub'
      this.connection = new signalR.HubConnectionBuilder().withAutomaticReconnect().withUrl(hubUrl).build()
      await this.connection.start().catch(err => alert(err.message))
      this.connection.invoke('RobotLogSubs', this.robotId).catch(err => console.error(err.toString()))
      this.connection.on('robotLog', p => {
        this.logs.unshift(p)
        if (this.logs.length > 100) {
          this.logs.pop()
        }
      })
    }

  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

